package com.yoshino.book.service.impl;

import com.yoshino.book.dao.BookDAO;
import com.yoshino.book.dao.CartItemDAO;
import com.yoshino.book.dao.OrderDAO;
import com.yoshino.book.dao.OrderItemDAO;
import com.yoshino.book.pojo.*;
import com.yoshino.book.service.BookService;
import com.yoshino.book.service.OrderService;

import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {

    private OrderDAO orderDAO;
    private OrderItemDAO orderItemDAO;
    private CartItemDAO cartItemDAO;
    private Integer getOrderTotalBookCount;
    private BookDAO bookDAO;
    private BookService bookService;

    @Override
    public void addOrderBean(OrderBean orderBean) {
        //1) 订单表添加一条记录
        //2) 订单详情表添加7条记录
        //3) 购物车项表中需要删除对应的7条记录
        //第一步
        orderDAO.addOrderBean(orderBean);   //执行完这一步，orderBean中的id是有值的
        //第二步
        //orderBean中的orderItemList是空的，此处应该根据用户的购物车中的购物车项去转换成一个一个的订单项
        //同时更新书库的信息
        User currUser = orderBean.getOrderUser();
        Map<Integer, CartItem> cartItemMap = currUser.getCart().getCartItemMap();
        for (CartItem cartItem: cartItemMap.values()){
            OrderItem orderItem = new OrderItem();
            orderItem.setBook(cartItem.getBook());
            orderItem.setBuyCount(cartItem.getBuyCount());
            orderItem.setOrderBean(orderBean);
            orderItemDAO.addOrderItem(orderItem);
            Book book = cartItem.getBook();
            bookDAO.updateBook(new Book(book.getId(), book.getSaleCount() + cartItem.getBuyCount(),book.getBookCount() - cartItem.getBuyCount()));
        }

        //第三步
        for (CartItem cartItem: cartItemMap.values()){
            cartItemDAO.delCartItem(cartItem);
        }
    }

    @Override
    public List<OrderBean> getOrderList(User user) {
        List<OrderBean> orderBeanList = orderDAO.getOrderList(user);

        for (OrderBean orderBean:orderBeanList){
            Integer totalBookCount = orderDAO.getOrderTotalBookCount(orderBean);
            orderBean.setTotalBookCount(totalBookCount);
        }
        return orderBeanList;

    }

    @Override
    public List<OrderBean> getOrderListByManager() {
        List<OrderBean> orderListByManager = orderDAO.getOrderListByManager();

        for (OrderBean orderBean:orderListByManager){
            Integer totalBookCount = orderDAO.getOrderTotalBookCount(orderBean);
            orderBean.setTotalBookCount(totalBookCount);
        }
        return orderListByManager;
    }

    @Override
    public void changeStatus(String orderNo, Integer orderStatus) {
        orderDAO.changeStatus(orderNo,orderStatus);
    }

    @Override
    public OrderBean getOrderBean(Integer id) {
        OrderBean orderBean = orderDAO.getOrderBean(id);

        List<OrderItem> orderList = orderItemDAO.getOrderList(id);
        for (OrderItem orderItem : orderList){
            Book book = bookService.getBook(orderItem.getBook().getId());
            orderItem.setBook(book);
        }
        orderBean.setOrderItemList(orderList);
        return orderBean;
    }
}
